package com.order.controller.sys;

import java.util.List;
import java.util.Map;
import com.order.interceptor.sys.UserInterceptor;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StringKit;
import com.order.config.Consts;
import com.order.model.sys.SysUser;
import com.order.model.sys.SysUserRole;
import com.order.shiro.ShiroUtils;

@ControllerBind(controllerKey = Consts.SYS + "user")
public class UserController extends Controller {


	private String message = "";
	private SysUser user = new SysUser();
	

	/** 用户主页	 */
	public void index() {
		render(Consts.SYS + "user.htm");
	}

	
	/** 保存、更新，存在则更新不存在则保存	 */
	@Before(UserInterceptor.class)
	public void saveOrUpdate() {
		user.getObj(getPara("json"));
		if (user.get(SysUser.ID).equals("")) {
			if(user.mySave())
				message = "保存成功!";
		} else {
			if (user.myUpdate()) 
				message = "修改成功！";
		}
		setAttr("msg", "温馨提示："+message);
		renderJson();
	}

	/**根据用户ID单行删除, 用户存在角色信息不允许删除*/
	public void delete() {
		int userid = getParaToInt(0);
		if (0 < userid) {
			if(SysUserRole.dao.isAuthorizeRole(userid) == true){
				message = "用户存在角色信息，请先移除角色信息";
			}else{
				user.deleteById(userid);
				message = "删除成功!";
			}
		} else {
			message = "未找到该记录,请选择要删的行。";
		}
		setAttr("msg", "温馨提示：" + message);
		renderJson();
	}

	/** 修改密码，先验证旧密码，再update新密码*/
	public void password(){		
		String json = getPara("json");
		JSONObject jsonObj = JSONObject.parseObject(json);// 将字符串转化成json对象
		String password = jsonObj.getString("password");// 获取字符串。
		String newpassword = jsonObj.getString("newpassword");
		String repassword = jsonObj.getString("repassword");
		
		if ("".equals(newpassword) || "".equals(repassword) || (! newpassword.equals(repassword))){
			message ="两次输入的密码不一致！";
		} else if("".equals(password) || !user.validatorpwd(password, ShiroUtils.getUserId())){
			message ="旧密码错误！";
		}else{
			user.password(newpassword, ShiroUtils.getUserId());
			message ="修改成功！";
		}
		System.out.println(message);
		setAttr("msg","温馨提示："+ message);
		renderJson();
	}

	/** 用户查询方法，如果没有条件则查询所有*/
	public void getUserJson(){
		String jsonStr = "{\"NAME\":\"\", \"STATUS\":\"\", \"MAIL\":\"\"}";
		int pagesize = (int) getParaToInt("pagesize");
		int page = (int) getParaToInt("page");
		JSONObject jsonObj = null;
		Map<String, Object> map = null;
		String condition = getPara("condition","");
		String wherejson = condition.replace("[", "").replace("]", "");		
		if (StringKit.notBlank(wherejson)) {			
			jsonObj = JSONObject.parseObject(wherejson);
			String field = jsonObj.getString("field");
			String value = jsonObj.getString("value");
			jsonStr = "{\"" + field + "\":\"" + value + "\"}";
			jsonObj = JSONObject.parseObject(jsonStr);
		}else{
			jsonObj = JSONObject.parseObject(getPara("where", jsonStr));			
		}
		map = SysUser.dao.getUserJson(page, pagesize, jsonObj);
		renderJson(map);
	}
		
	/**查询所有用户用于下拉框*/
	public void getUserCombobox() {
		List<SysUser> role = SysUser.dao.find("select * from sys_user");
		renderJson(role);
	}
	
}
